package com.martain.leetcode.t99;

import com.martain.leetcode.common.TreeNode;

public class t99 {

    TreeNode pre = null;

    TreeNode err1 = null;

    TreeNode err2 = null;

    public void recoverTree(TreeNode root) {
        if (root==null) return;
        ldr(root);
        Integer tmp = err1.val;
        err1.val = err2.val;
        err2.val = tmp;
    }

    public void ldr(TreeNode node){
        if (node==null) return;
        ldr(node.left);
        if (pre!=null && pre.val>node.val){
            if (err1 ==null){
                err1 = pre;
            }else {
                err2 = pre;
            }
        }
        pre = node;
        ldr(node.right);
    }



}
